<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class pre_order extends AdminController{
	public $_validation = null;

	function __construct(){
		parent::__construct();
		$this->load->model('MWxPay');
        $this->load->model('MUser');
        $allUser = $this->MUser->getAllUser();

        $this->assign('allUser',$allUser);
	}

	function index(){
		$this->load->library('FormFilter');
		$this->load->helper('formfilter');
		$this->formfilter->addFilter('w.status','where');
		$this->formfilter->addFilter('w.out_trade_no','like');
		$this->formfilter->addFilter('w.user_id','where');
        if ($begintime = filterValue('order_time_begin')) {
            $this->formfilter->addFilter('order_time_begin', 'where', array('last_return_time >= ', strtotime($begintime . ' 00:00:00')));
        }
        if ($endtime = filterValue('order_time_end')) {
            $this->formfilter->addFilter('order_time_end', 'where', array('last_return_time <= ', strtotime($endtime . ' 23:59:59')));
        }
        $number_perpage = filterValue('order_number_perpage');
        $total_number = $this->MWxPay->getCount();
        $limit = $this->pagination($total_number,'',$number_perpage);
        $list  = $this->MWxPay->getList($limit);
		$this->assign('list', $list);
		$this->layout();
	}

    /**
     * ajax 查找给定id的任务操作详情
     * @param $id
     */
    function ajaxViewDetail($id)
    {
        if (((int)$id) . '' !== $id) {
            model_error(l('id_or_updated_not_null'));
        }
        if (!$obj = $this->MOrder->getById((int)$id)) {
            model_error('订单不存在！');
        }

        //查询当前测评记录所有款项记录
        $this->load->model('MTaskRecordItem');
        $detail = $this->MOrder->getDetail($id);

        $this->assign('detail', $detail);
        $this->assign('thisControllerName', '订单详情');
        $this->layout_modal('modal-detail');
    }

    public function return_confirm(){
        $data = $this->input->post();
        $item = $this->MOrder->getById($data['oid']);
        if ((int)$item->status !== 1){
            exit(json_encode([
                'status' => 0,
                'msg' => '只有申请还车的订单可以进行确认还车操作',
                'data' => $item
            ]));
        }
        if ($this->db->update('order',['status' => 2, 'return_time' => time()],['id' => $data['oid']]) && $this->db->update('car',['status' => 0],['id' => $item->car_id])){
            exit(json_encode([
                'status' => 1,
                'msg' => '确认还车成功'
            ]));
        }else{
            exit(json_encode([
                'status' => 0,
                'msg' => '确认还车失败'
            ]));
        }
    }

}
